package MULE.Model.Panel;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;

import javax.swing.*;

import MULE.Model.Logic.GameConfigLogic;

/**
 * This class creates the game configuration screen for a game of M.U.L.E.
 * 
 * It gives the player several options, including difficulty, map type, and
 * number of players, and moves to the player configuration screen after this
 * selection is completed.
 * 
 * @ author William Baldwin @ version 1.0 10.07.2013
 * 
 */
@SuppressWarnings("serial")
public class GameConfigPanel extends JPanel {

	// Button groups for each configuration item.
	private final ButtonGroup Difficulty = new ButtonGroup();
	private final ButtonGroup MapType = new ButtonGroup();
	private final ButtonGroup PlayerNum = new ButtonGroup();

	private JButton nextButton;
	private ImageIcon mule;
	private JLabel image, text;
	private int numPlayers = 2;

	/**
	 * Creates and builds the game configuration panel.
	 * 
	 * @param logicModule
	 *            The presenter object responsible for processing panel inputs.
	 */
	public GameConfigPanel() {
		// Use absolute layout -- allows for more freedom of positioning.
		setLayout(null);
		this.setBackground(Color.yellow);
		// Label and buttons for difficulty settings.
		JLabel diffLabel = new JLabel("Difficulty");
		diffLabel.setBounds(80, 200, 150, 14);
		add(diffLabel);

		JRadioButton beginner = new JRadioButton("Beginner");
		beginner.setActionCommand("Beginner");
		Difficulty.add(beginner);
		beginner.setBounds(70, 220, 109, 23);
		add(beginner);

		JRadioButton standard = new JRadioButton("Standard");
		standard.setActionCommand("Standard");
		Difficulty.add(standard);
		standard.setBounds(70, 240, 109, 23);
		add(standard);

		JRadioButton tournament = new JRadioButton("Tournament");
		tournament.setActionCommand("Tournament");
		Difficulty.add(tournament);
		tournament.setBounds(70, 260, 109, 23);
		add(tournament);

		// Label and buttons for map settings.
		JLabel label_1 = new JLabel("Map Type");
		label_1.setBounds(220, 200, 150, 14);
		add(label_1);

		mule = new ImageIcon("mule.png");
		image = new JLabel(mule);
		image.setBackground(Color.YELLOW);
		image.setBounds(20, 20, 500, 200);
		add(image);

		text = new JLabel("M.U.L.E.");
		text.setFont(new Font("Princetown LET", Font.BOLD, 72));
		text.setBounds(130, 0, 500, 200);
		add(text);

		JRadioButton defaultMap = new JRadioButton("Default");
		defaultMap.setActionCommand("Default");
		MapType.add(defaultMap);
		defaultMap.setBounds(210, 220, 109, 23);
		add(defaultMap);

		JRadioButton randomMap = new JRadioButton("Random");
		randomMap.setActionCommand("Random");
		MapType.add(randomMap);
		randomMap.setBounds(210, 240, 109, 23);
		add(randomMap);

		// Label and buttons for player number settings.
		JLabel label_2 = new JLabel("Number of Players");
		label_2.setBounds(360, 200, 200, 14);
		add(label_2);

		JRadioButton players2 = new JRadioButton("2");
		players2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				numPlayers = 2;
			}
		});
		PlayerNum.add(players2);
		players2.setBounds(350, 220, 50, 23);
		add(players2);

		JRadioButton players3 = new JRadioButton("3");
		players3.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				numPlayers = 3;
			}
		});
		PlayerNum.add(players3);
		players3.setBounds(350, 240, 50, 23);
		add(players3);

		JRadioButton players4 = new JRadioButton("4");
		players4.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				numPlayers = 4;
			}
		});
		PlayerNum.add(players4);
		players4.setBounds(350, 260, 50, 23);
		add(players4);

		// "Next" button. Processes input data and goes to next panel on click.
		nextButton = new JButton("Next");
		String nextCommand = GameConfigLogic.ActionCommand.NEXT.toString();
		nextButton.setActionCommand(nextCommand);
		nextButton.setBounds(30, 300, 500, 41);
		add(nextButton);
	}

	/**
	 * Returns the selection made in the difficulty section.
	 * 
	 * @return Difficulty selection.
	 */
	public ButtonModel getDifficultySelection() {
		return Difficulty.getSelection();
	}

	/**
	 * Returns the selection made in the map type section.
	 * 
	 * @return Map type selection.
	 */
	public ButtonModel getMapTypeSelection() {
		return MapType.getSelection();
	}

	/**
	 * Returns the selection made in the player number section.
	 * 
	 * @return Player number selection.
	 */
	public ButtonModel getPlayerNumSelection() {
		return PlayerNum.getSelection();
	}

	/**
	 * Returns the number of players selected.
	 * 
	 * @return Number of players selection.
	 */
	public int getNumPlayers() {
		return numPlayers;
	}

	/**
	 * Adds given action listener to the panel's next button.
	 * 
	 * @param listener
	 *            Listener to add to next button.
	 */
	public void addNextButtonListener(ActionListener listener) {
		nextButton.addActionListener(listener);
	}
}